mui.init({
	beforeback: function(){
//		alert(plus.webview.getLaunchWebview().id);
//		var indexPage = plus.webview.getLaunchWebview();
//		var indexPageId = indexPage.id;
//		mui.fire(indexPage,'refresh',{});
		return true;
	}
});

var old_back = mui.back;
mui.back = function(){
 // var btn = ["确定","取消"];
 // mui.confirm('确认关闭当前窗口？','提示',btn,function(e){
  //  if(e.index==0){
  	//	plus.webview.currentWebview().close();
	    	var indexPage = plus.webview.getLaunchWebview();
//	    	plus.storage.setItem(KeyCfg.currClickTabId,"userAccount");
	    indexPage.show();
//	    mui.fire(indexPage,'refresh',{});
        indexPage.evalJS("openTab(null,subpages[2]);refreshSubPages('userAccount');"); 
//    }
 // });
}

mui.plusReady(function() {
	
	/*---------- 完成实名认证，下一步 提交事件 CODE START ---------*/
	var btn_finish = CmwMcore.$('btn_finish');
	MeventMgr.onTouchstart(btn_finish,function(event,self){
		var disabled = $("#btn_finish").prop("disabled");
		if(disabled) return;
		if(!valid()) return;
		submitForm();
	});

	function valid(){
		var name_val = $("#name").val();
		var cardid_val = $("#cardid").val();
		var nickname_val = $("#nickname").val();
		if(!name_val || !name_val.trim()){
			mui.alert("请输入真实姓名!");
			return false;
		}
		
		if(!cardid_val || !cardid_val.trim()){
			mui.alert("请输入您的身份证号!");
			return false;
		}
		
		var statuss = false;
		card = cardid_val.trim();
		if (card.length == 15 || card.length == 18) {
			//中国身份证 15位或者18位
			statuss = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(card);
		} else if (/(^[A-Z]$)/.test(card.substr(0, 1))) {
			//香港身份证以字母开头，只验证是否以字母开头，不验证长度
			statuss = true;
		} else if (card.length == 8) {
			//港澳身份证
			statuss = /(^[A-Z0-9]\d{6}[A-Z0-9]$)/.test(card);
		}
		if(!statuss){
			mui.alert("身份证号码格式错误!");
			return false; 
		}
		
		if(!nickname_val || !nickname_val.trim()){
			mui.alert("请输入昵称!");
			return false;
		}
		return true;
	}
	
	function submitForm(){
		CmwMcore.disabled('btn_finish');
		var formDatas = $("#theForm").serialize();
		var sessionId = plus.storage.getItem(KeyCfg.JSESSIONID);
		formDatas.sessionId = sessionId;
		var url = UrlConfig.mproving_saveCardId;
		MuiEventMgr.ajax(url,{pars:formDatas,
		  	sfn : function(data){
		  		callback(data);
	        		CmwMcore.enabled('btn_finish');
	        		$("#btn_finish").css("background","#0B56A7")
		  	},ffn : function(){
		  		CmwMcore.enabled('btn_finish');
		  	}
		 });
	 
//		$.ajax(url, {  
//	        data : formDatas,  
//	        dataType : 'jsonp',  
//	        crossDomain: true,  
//	        success: function(data) {
//	        		data = JSON.parse(data);
//	        		console.log("success="+data.success);
//	        		console.log("msg="+data.msg);
//	        		callback(data);
//	        		CmwMcore.enabled('btn_finish');
//	        },
//	        error :function(){
//	        		CmwMcore.enabled('btn_finish');
//	        }
//	    });
	}
	
	function callback(data){
		var success = data.success;
		var msg = data.msg;
		if(!success){
			mui.alert(msg);
			return;
		}
		forward(data);
	}
	
	function forward(data){
		MuiEventMgr.checkSecuity(function(){
			var webvewId = plus.webview.currentWebview().id;
			plus.storage.setItem("srealNameId",webvewId);
		});
	}
	/*---------- 完成实名认证，下一步 提交事件 CODE END ---------*/

});